import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsOptional,
  IsBoolean,
  IsObject,
  IsEnum,
  IsNumber,
  IsUUID,
  Length,
  Min,
  Max,
} from 'class-validator';
// import { Transform } from 'class-transformer';

export class JoinRoomMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiPropertyOptional({ description: '房间密码' })
  @IsOptional()
  @IsString()
  @Length(4, 50)
  password?: string;

  @ApiPropertyOptional({ description: '用户昵称' })
  @IsOptional()
  @IsString()
  @Length(1, 50)
  nickname?: string;

  @ApiPropertyOptional({ description: '用户头像URL' })
  @IsOptional()
  @IsString()
  avatar?: string;

  @ApiPropertyOptional({ description: '媒体设备信息' })
  @IsOptional()
  @IsObject()
  mediaDevices?: {
    audioInputId?: string;
    videoInputId?: string;
    audioOutputId?: string;
    hasAudioInput?: boolean;
    hasVideoInput?: boolean;
  };

  @ApiPropertyOptional({ description: '用户元数据' })
  @IsOptional()
  @IsObject()
  metadata?: {
    browser?: string;
    platform?: string;
    deviceType?: 'desktop' | 'mobile' | 'tablet';
    timezone?: string;
    language?: string;
    [key: string]: any;
  };
}

export class LeaveRoomMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiPropertyOptional({ description: '离开原因' })
  @IsOptional()
  @IsString()
  @Length(1, 200)
  reason?: string;
}

export class MediaStateMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiPropertyOptional({ description: '音频状态' })
  @IsOptional()
  @IsBoolean()
  audioEnabled?: boolean;

  @ApiPropertyOptional({ description: '视频状态' })
  @IsOptional()
  @IsBoolean()
  videoEnabled?: boolean;

  @ApiPropertyOptional({ description: '屏幕共享状态' })
  @IsOptional()
  @IsBoolean()
  screenShareEnabled?: boolean;

  @ApiPropertyOptional({ description: '静音状态' })
  @IsOptional()
  @IsBoolean()
  isMuted?: boolean;
}

export class SignalingMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '发送者用户ID' })
  @IsString()
  @Length(1, 100)
  fromUserId: string;

  @ApiProperty({ description: '目标用户ID' })
  @IsString()
  @Length(1, 100)
  targetUserId: string;

  @ApiProperty({
    description: '信令类型',
    enum: ['offer', 'answer', 'ice-candidate', 'ice-candidate-error'],
  })
  @IsEnum(['offer', 'answer', 'ice-candidate', 'ice-candidate-error'])
  type: 'offer' | 'answer' | 'ice-candidate' | 'ice-candidate-error';

  @ApiProperty({ description: '信令载荷' })
  @IsObject()
  payload: any;
}

export class ChatMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiProperty({ description: '消息内容' })
  @IsString()
  @Length(1, 1000)
  content: string;

  @ApiPropertyOptional({ description: '用户昵称' })
  @IsOptional()
  @IsString()
  @Length(1, 50)
  nickname?: string;

  @ApiPropertyOptional({
    description: '消息类型',
    enum: ['text', 'system'],
    default: 'text',
  })
  @IsOptional()
  @IsEnum(['text', 'system'])
  type?: 'text' | 'system';

  @ApiPropertyOptional({ description: '消息元数据' })
  @IsOptional()
  @IsObject()
  metadata?: {
    mentions?: string[]; // 提及的用户ID列表
    replyTo?: string; // 回复的消息ID
    attachments?: string[]; // 附件URL列表
    [key: string]: any;
  };
}

export class NetworkQualityMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiPropertyOptional({ description: '往返时间(ms)' })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(10000)
  rtt?: number;

  @ApiPropertyOptional({ description: '丢包率(0-1)' })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(1)
  packetLoss?: number;

  @ApiPropertyOptional({ description: '带宽(kbps)' })
  @IsOptional()
  @IsNumber()
  @Min(0)
  bandwidth?: number;

  @ApiPropertyOptional({
    description: '网络质量等级',
    enum: ['excellent', 'good', 'fair', 'poor'],
  })
  @IsOptional()
  @IsEnum(['excellent', 'good', 'fair', 'poor'])
  quality?: 'excellent' | 'good' | 'fair' | 'poor';

  @ApiPropertyOptional({ description: '额外的网络统计信息' })
  @IsOptional()
  @IsObject()
  stats?: {
    jitter?: number; // 抖动
    bitrate?: number; // 比特率
    frameRate?: number; // 帧率
    resolution?: string; // 分辨率
    codecName?: string; // 编解码器名称
    [key: string]: any;
  };
}

export class RoomEventMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({
    description: '事件类型',
    enum: [
      'participant-joined',
      'participant-left',
      'participant-media-changed',
      'room-settings-changed',
      'room-ended',
      'host-changed',
    ],
  })
  @IsEnum([
    'participant-joined',
    'participant-left',
    'participant-media-changed',
    'room-settings-changed',
    'room-ended',
    'host-changed',
  ])
  eventType: string;

  @ApiProperty({ description: '事件数据' })
  @IsObject()
  data: any;

  @ApiPropertyOptional({ description: '触发事件的用户ID' })
  @IsOptional()
  @IsString()
  triggeredBy?: string;
}

export class ScreenShareMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiProperty({ description: '屏幕共享状态' })
  @IsBoolean()
  enabled: boolean;

  @ApiPropertyOptional({ description: '共享类型' })
  @IsOptional()
  @IsEnum(['screen', 'window', 'tab'])
  shareType?: 'screen' | 'window' | 'tab';

  @ApiPropertyOptional({ description: '共享配置' })
  @IsOptional()
  @IsObject()
  config?: {
    audio?: boolean; // 是否包含音频
    cursor?: 'always' | 'motion' | 'never'; // 光标显示模式
    displaySurface?: 'monitor' | 'window' | 'browser'; // 显示表面类型
  };
}

export class FileShareMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '发送者用户ID' })
  @IsString()
  @Length(1, 100)
  fromUserId: string;

  @ApiPropertyOptional({ description: '目标用户ID（私发）' })
  @IsOptional()
  @IsString()
  @Length(1, 100)
  toUserId?: string;

  @ApiProperty({ description: '文件信息' })
  @IsObject()
  fileInfo: {
    name: string;
    size: number;
    type: string;
    url?: string; // 文件下载URL
    chunks?: number; // 分片数量
    checksum?: string; // 文件校验和
  };

  @ApiPropertyOptional({
    description: '传输类型',
    enum: ['direct', 'relay'],
    default: 'relay',
  })
  @IsOptional()
  @IsEnum(['direct', 'relay'])
  transferType?: 'direct' | 'relay';
}

export class RecordingMessageDto {
  @ApiProperty({ description: '房间ID' })
  @IsString()
  @IsUUID()
  roomId: string;

  @ApiProperty({ description: '操作者用户ID' })
  @IsString()
  @Length(1, 100)
  userId: string;

  @ApiProperty({
    description: '录制操作',
    enum: ['start', 'stop', 'pause', 'resume'],
  })
  @IsEnum(['start', 'stop', 'pause', 'resume'])
  action: 'start' | 'stop' | 'pause' | 'resume';

  @ApiPropertyOptional({ description: '录制配置' })
  @IsOptional()
  @IsObject()
  config?: {
    includeAudio?: boolean;
    includeVideo?: boolean;
    includeScreenShare?: boolean;
    quality?: 'low' | 'medium' | 'high';
    format?: 'webm' | 'mp4';
  };
}

export class ErrorMessageDto {
  @ApiProperty({ description: '错误类型' })
  @IsString()
  type: string;

  @ApiProperty({ description: '错误消息' })
  @IsString()
  message: string;

  @ApiPropertyOptional({ description: '错误代码' })
  @IsOptional()
  @IsString()
  code?: string;

  @ApiPropertyOptional({ description: '错误详情' })
  @IsOptional()
  @IsObject()
  details?: any;

  @ApiProperty({ description: '时间戳' })
  timestamp: Date;
}
